<template>
  <div
    class="vui-loading"
    :style="{
      '--front-color': frontColor,
      '--back-color': backColor,
      '--text-color': textColor,
    }"
  >
    <svg class="circle-outer" viewBox="0 0 86 86">
      <circle class="back" cx="43" cy="43" r="40" />
      <circle class="front" cx="43" cy="43" r="40" />
      <circle class="new" cx="43" cy="43" r="40" />
    </svg>
    <svg class="circle-middle" viewBox="0 0 60 60">
      <circle class="back" cx="30" cy="30" r="27" />
      <circle class="front" cx="30" cy="30" r="27" />
    </svg>
    <svg class="circle-inner" viewBox="0 0 34 34">
      <circle class="back" cx="17" cy="17" r="14" />
      <circle class="front" cx="17" cy="17" r="14" />
    </svg>
    <div class="text" :data-text="text"></div>
  </div>
</template>

<script>
  import { defineComponent } from 'vue'

  export default defineComponent({
    name: 'VuiLoading',
    props: {
      text: {
        type: String,
        default: '加载中...',
      },
      frontColor: {
        type: String,
        default: '#1e90ff',
      },
      backColor: {
        type: String,
        default: '#c3c8de',
      },
      textColor: {
        type: String,
        default: '#414856',
      },
    },
    setup() {
      return {}
    },
  })
</script>

<style lang="scss" scoped>
  .vui-loading {
    --front-color: #1e90ff;
    --back-color: #c3c8de;
    --text-color: #414856;
    width: 64px;
    height: 64px;
    border-radius: 50px;
    position: relative;
    display: -webkit-box;
    display: flex;
    -webkit-box-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    align-items: center;
  }
  .vui-loading svg {
    position: absolute;
    display: -webkit-box;
    display: flex;
    -webkit-box-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    align-items: center;
  }
  .vui-loading svg circle {
    position: absolute;
    fill: none;
    stroke-width: 6px;
    stroke-linecap: round;
    stroke-linejoin: round;
    -webkit-transform: rotate(-100deg);
    transform: rotate(-100deg);
    -webkit-transform-origin: center;
    transform-origin: center;
  }
  .vui-loading svg circle.back {
    stroke: var(--back-color);
  }
  .vui-loading svg circle.front {
    stroke: var(--front-color);
  }
  .vui-loading svg.circle-outer {
    height: 86px;
    width: 86px;
  }
  .vui-loading svg.circle-outer circle {
    stroke-dasharray: 62.75 188.25;
  }
  .vui-loading svg.circle-outer circle.back {
    -webkit-animation: circle-outer 1.8s ease infinite 0.3s;
    animation: circle-outer 1.8s ease infinite 0.3s;
  }
  .vui-loading svg.circle-outer circle.front {
    -webkit-animation: circle-outer 1.8s ease infinite 0.15s;
    animation: circle-outer 1.8s ease infinite 0.15s;
  }
  .vui-loading svg.circle-middle {
    height: 60px;
    width: 60px;
  }
  .vui-loading svg.circle-middle circle {
    stroke-dasharray: 42.5 127.5;
  }
  .vui-loading svg.circle-middle circle.back {
    -webkit-animation: circle-middle 1.8s ease infinite 0.25s;
    animation: circle-middle 1.8s ease infinite 0.25s;
  }
  .vui-loading svg.circle-middle circle.front {
    -webkit-animation: circle-middle 1.8s ease infinite 0.1s;
    animation: circle-middle 1.8s ease infinite 0.1s;
  }
  .vui-loading svg.circle-inner {
    height: 34px;
    width: 34px;
  }
  .vui-loading svg.circle-inner circle {
    stroke-dasharray: 22 66;
  }
  .vui-loading svg.circle-inner circle.back {
    -webkit-animation: circle-inner 1.8s ease infinite 0.2s;
    animation: circle-inner 1.8s ease infinite 0.2s;
  }
  .vui-loading svg.circle-inner circle.front {
    -webkit-animation: circle-inner 1.8s ease infinite 0.05s;
    animation: circle-inner 1.8s ease infinite 0.05s;
  }
  .vui-loading .text {
    position: absolute;
    bottom: -40px;
    display: -webkit-box;
    display: flex;
    -webkit-box-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    align-items: center;
    text-transform: lowercase;
    font-weight: 500;
    font-size: 14px;
    letter-spacing: 0.2px;
  }
  .vui-loading .text::before,
  .vui-loading .text::after {
    content: attr(data-text);
  }
  .vui-loading .text::before {
    color: var(--text-color);
  }
  .vui-loading .text::after {
    color: var(--front-color);
    -webkit-animation: text-animation 3.6s ease infinite;
    animation: text-animation 3.6s ease infinite;
    position: absolute;
    left: 0;
  }

  @-webkit-keyframes circle-outer {
    0% {
      stroke-dashoffset: 25;
    }
    25% {
      stroke-dashoffset: 0;
    }
    65% {
      stroke-dashoffset: 301;
    }
    80% {
      stroke-dashoffset: 276;
    }
    100% {
      stroke-dashoffset: 276;
    }
  }

  @keyframes circle-outer {
    0% {
      stroke-dashoffset: 25;
    }
    25% {
      stroke-dashoffset: 0;
    }
    65% {
      stroke-dashoffset: 301;
    }
    80% {
      stroke-dashoffset: 276;
    }
    100% {
      stroke-dashoffset: 276;
    }
  }
  @-webkit-keyframes circle-middle {
    0% {
      stroke-dashoffset: 17;
    }
    25% {
      stroke-dashoffset: 0;
    }
    65% {
      stroke-dashoffset: 204;
    }
    80% {
      stroke-dashoffset: 187;
    }
    100% {
      stroke-dashoffset: 187;
    }
  }
  @keyframes circle-middle {
    0% {
      stroke-dashoffset: 17;
    }
    25% {
      stroke-dashoffset: 0;
    }
    65% {
      stroke-dashoffset: 204;
    }
    80% {
      stroke-dashoffset: 187;
    }
    100% {
      stroke-dashoffset: 187;
    }
  }
  @-webkit-keyframes circle-inner {
    0% {
      stroke-dashoffset: 9;
    }
    25% {
      stroke-dashoffset: 0;
    }
    65% {
      stroke-dashoffset: 106;
    }
    80% {
      stroke-dashoffset: 97;
    }
    100% {
      stroke-dashoffset: 97;
    }
  }
  @keyframes circle-inner {
    0% {
      stroke-dashoffset: 9;
    }
    25% {
      stroke-dashoffset: 0;
    }
    65% {
      stroke-dashoffset: 106;
    }
    80% {
      stroke-dashoffset: 97;
    }
    100% {
      stroke-dashoffset: 97;
    }
  }
  @-webkit-keyframes text-animation {
    0% {
      -webkit-clip-path: inset(0 100% 0 0);
      clip-path: inset(0 100% 0 0);
    }
    50% {
      -webkit-clip-path: inset(0);
      clip-path: inset(0);
    }
    100% {
      -webkit-clip-path: inset(0 0 0 100%);
      clip-path: inset(0 0 0 100%);
    }
  }
  @keyframes text-animation {
    0% {
      -webkit-clip-path: inset(0 100% 0 0);
      clip-path: inset(0 100% 0 0);
    }
    50% {
      -webkit-clip-path: inset(0);
      clip-path: inset(0);
    }
    100% {
      -webkit-clip-path: inset(0 0 0 100%);
      clip-path: inset(0 0 0 100%);
    }
  }
</style>
